<?php
namespace App\Http\Controllers\background;
use App\Http\Controllers\Controller;
use App\Models\Admin\InterfaceConfiguration;
use App\Models\Admin\Supplier;
use App\Models\Admin\SupplierInterfacePermissions;
use App\Models\Mysql\{XnGoodsCarmyType,XnOrder,XnSupplierGoods,XnTerraceInfo};
use App\Services\Terrace\TerraceService;
use Illuminate\Http\Request;

class InterfaceController extends Controller
{
    public function Interfacelist(TerraceService $terraceService,Request $request){
        $name = $request -> post('inter_name','');
        $supplier_id = $request -> post('supplier_id','');
        if(empty($supplier_id)){
            return fails('请传参数');
        }
        $where = [];
        if(!empty($where)){
            $where = ['title','like','%'.$name.'%'];
        }
        $ins_id = SupplierInterfacePermissions::where('sid','=',$supplier_id)->pluck('interface_id');
        if($ins_id){
            $interface_list = InterfaceConfiguration::whereIn('id',$ins_id)->where($where)
                -> select('id','en','title')
                -> get()
                -> toArray();
            if($interface_list){
                foreach($interface_list as $k => $v){
                    if(XnTerraceInfo::where('sid',$v['id']) -> doesntExist()){
                        $arr = ['sid'=>$v['id']];
                        XnTerraceInfo::insert($arr);
                        $interface_list[$k]['datas'] = [];
                    }else{
                        $interface_list[$k]['datas'] = json_decode(XnTerraceInfo::where('sid',$v['id']) -> value('datas'),true);
                    }
                }
            }
        }else{
            return fails('未查询到数据');
        }
        return successly($interface_list);
    }

    public function interfaceset(Request $request,TerraceService $terraceService){
        $app_id = $request -> post('inter_name','');
        $key = $request -> post('key','');
        $url = $request -> post('url','');
        $s_id = $request -> post('s_id','');
        if(empty($app_id)){
            return  fails('请传账号');
        }
        if(empty($key)){
            return  fails('请传秘钥');
        }
        if(empty($s_id)){
            return  fails('请传参数');
        }
        $terraceService -> update($app_id,$key,$url,$s_id);
        return  successly();
    }

    public function interfacebalance(Request $request){
        $sid = $request -> post('s_id','');
        $balanceController = new BalanceController();
        $supplier_id = $request -> post('supplier_id','');
        if(empty($supplier_id)){
            return fails('请传参数');
        }
        $ins_id = SupplierInterfacePermissions::where('sid','=',$supplier_id)->pluck('interface_id');
        if($ins_id){
            if(!empty($sid)){
                $sid_info = InterfaceConfiguration::whereIn('id',$ins_id)->where('id',$sid)
                    -> select('id','en','title')
                    -> first();
                if($sid_info){
                    $sid_info_arr = $sid_info -> toArray();
                    $user_data = XnTerraceInfo::where('sid',$sid)->value('datas');
                    if($user_data){
                        $user_data_arr = json_decode($user_data,true);
                        $sid_info_arr['balance'] = $this -> getSupplierBalance($sid,$user_data_arr,$balanceController);
                    }else{
                        $sid_info_arr['balance'] = 0;
                    }
                    return  successly($sid_info_arr);
                }else{
                    return  fails('未查询到数据');
                }
            }else{
                $interface_list = InterfaceConfiguration::whereIn('id',$ins_id)
                    -> select('id','en','title')
                    -> get()
                    -> toArray();
                if($interface_list){
                    foreach($interface_list as $k => $v){
                        if(XnTerraceInfo::where('sid',$v['id']) -> doesntExist()){
                            $arr = ['sid'=>$v['id']];
                            XnTerraceInfo::insert($arr);
                            $interface_list[$k]['balance'] = 0;
                        }else{
                            $user_datass = XnTerraceInfo::where('sid',$v['id']) -> value('datas');
                            if(!empty($user_datass)){
                                $user_res_arrs = json_decode($user_datass,true);
                                $interface_list[$k]['balance'] = $this -> getSupplierBalance($v['id'],$user_res_arrs,$balanceController);
                            }else{
                                $interface_list[$k]['balance'] =0;
                            }
                        }
                    }
                    return successly($interface_list);
                }else{
                    return fails('未查询到数据');
                }
            }
        }else{
            return fails('未查询到数据');
        }
    }

    public function interfacedetail(Request $request){
        $start_time = $request -> post('start_time','');
        $end_time = $request -> post('end_time','');
        $goods_name = $request -> post('goods_name','');
        $order_sn = $request -> post('order_sn','');
        $order_status = $request -> post('order_status','');
        $s_id = $request -> post('sid','');
        if(empty($s_id)){
            return  fails('请传参数');
        }
        $condition[] = ['goods_belong','=',$s_id];
        $condition[] = ['recharge_time','>=',$start_time];
        $condition[] = ['recharge_time','<=',$end_time];
        if(!empty($goods_name)){
            $condition[] = ['goods_name','like','%'.$goods_name.'%'];
        }
        if(!empty($order_sn)){
            $condition[] = ['order_sn','=',$order_sn];
        }
        if(!empty($order_status)){
            $condition[] = ['recharge_status','=',$order_status];
        }else{
            $condition[] = ['recharge_status','!=',-1];
        }
        $res = XnOrder::where($condition)
            -> selectRaw('count(IF(recharge_status = 1,1,NULL)) as successorder,count(IF(recharge_status = 2,1,NULL)) as failsorder,sum(IF(recharge_status = 1,unitPrice*goods_num,null)) as successmoney,sum(if(recharge_status = 2,unitPrice*goods_num,null)) as failsmoney')
            -> first();
        $allCount = ($res -> successorder) + ($res -> failsorder);
        $data = [
            'allcount'=> $allCount,
            'succount'=> $res -> successorder,
            'falcount'=> $res -> failsorder,
            'sucmoney'=> $res -> successmoney ?? 0.00,
            'falmoney'=> $res -> failsmoney ?? 0.00
        ];
        $info =  XnOrder::where($condition)
            -> select('recharge_time','mechant_name','goods_name','order_sn','unitPrice','account','goods_num')
            -> orderBy('recharge_time','desc')
            -> paginate(10)
           -> toArray();
        $info['revange'] = $data;
        return successly($info);
    }

    public function getSupplierBalance($sid,$user_data_arr,$balanceController){
        $balance = 0;
        switch ($sid){
            case 3:
                $balance =  $balanceController->getchengquanmoney($user_data_arr['app_id'],$user_data_arr['key']);
                break;
            case 4:
                $balance =  $balanceController->getfulumoney($user_data_arr['app_id'],$user_data_arr['key']);
                break;
            case 5:
                $balance =  $balanceController->getyunfakamoney($user_data_arr['app_id'],$user_data_arr['key'],$user_data_arr['url']);
                break;
            case 6:
                $balance =  $balanceController->getleigemoney($user_data_arr['app_id'],$user_data_arr['key']);
                break;
            case 8:
                $balance =  $balanceController->getchengquanmoney($user_data_arr['app_id'],$user_data_arr['key']);
                break;
            case 7:
                $balance =  $balanceController->getgongjiangmoney($user_data_arr['app_id'],$user_data_arr['key']);
                break;
        }
        return $balance;
    }

    public function CamiCate(Request $request){
        $types = $request -> post('type','');
        $goods_id = $request -> post('goods_id','');
        $name = $request -> post('name','');
        $status = $request -> post('status','');
        $condition = [];
        if($types == 1){
            $o_type = XnSupplierGoods::find($goods_id) -> o_type;
           // echo $o_type;exit;
            $condition[] = ['o_type','=',$o_type];
            $condition[] = ['is_open','=',1];
        }
        if(!empty($name)){
            $condition[] = ['name','like','%'.$name.'%'];

        }
        if(!empty($status)){
            $condition[] = ['is_open','=',$status];
        }
        $res = XnGoodsCarmyType::where($condition) -> orderBy('ctime','desc') -> get() -> toArray();
        if($res){
            return successly($res);
        }else{
            return  fails('暂无信息');
        }
    }

    public function CamiCateStatus(Request $request){
        $c_id = $request -> post('c_id');
        $status = $request -> post('is_open');
        if(empty($c_id) || empty($status)){
            return fails('请传参数');
        }
        $res = XnGoodsCarmyType::find($c_id);
        $res -> is_open = $status;
        $res -> save();
        return successly();
    }

    public function getCateInfo(Request $request){
        $c_id = $request -> post('c_id');
        if(empty($c_id)){
            return fails('请传参数');
        }
        $res = XnGoodsCarmyType::find($c_id);
        $cate = $res -> catrgory;
        $cate = json_decode($cate,true);
        return successly($cate);
    }
}
